<?php 
namespace frontend\controllers;

use frontend\models\Upload_img;

use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;

use frontend\models\Resume;
use yii\web\UploadedFile;


class ResumeController extends Controller
{
    public function init()
    {
        parent::init();
        session_start();
        $get=Yii::$app->request->get('r');
        if($get!='login/index')
        {
            if (!isset($_SESSION['user']) ){
                // echo "没有session";
                echo '<script>alert("请先登录");location.href="?r=login/index"</script>';
            }
        }
    }



	public $layout='index';
    public $enableCsrfValidation = false; // 关闭scrf

    public function actionIndex(){

        $user_id = $_SESSION['user']['user_id'];

        $model = new Upload_img();
        if($data = Yii::$app->request->isPost){

            $model->resume_img = UploadedFile::getInstance($model, 'resume_img');
            if( $resume_img = $model->upload() ){

                $resume = Resume::findOne(['user_id'=> $user_id]);

                $resume->resume_img = $resume_img;

                $resume->save();

                return $this->redirect(['resume/index']);

            }else{
                // 个人头像上传失败
                echo '个人头像上传失败';
            }



        }else{


            // 根据当前登录的用户去查询简历
            $resume = Resume::find()->where( ['user_id'=>$user_id])->one();
//            print_r($_SESSION);die;

//            print_r($resume);die;
            return $this->render('index', ['resume'=>$resume ,'model'=>$model]);
        }
    }


    // 预览简历
    public function actionShow_resume(){

        $user_id =  $_SESSION['user']['user_id']; // 当前登录的用户 id

        $user = Resume::findOne(['user_id'=>$user_id]);

        return $this->renderPartial('show_resume',['data'=>$user]);
    }



    // 修改  基本信息
    public function actionJbxi(){
        $data = Yii::$app->request->post();
        $user_id =  $_SESSION['user']['user_id'];
        //个人简历 表
        // 当亲登录的用户 id     从  session|cooike 中取 id
        $resume = Resume::find()->where( ['user_id'=>$user_id])->one();
        $resume->resume_name = $data['resume_name'];
        $resume->resume_sex = $data['resume_sex'];
        $resume->resume_degree = $data['resume_degree'];
        $resume->resume_experience = $data['resume_experience'];
        $resume->resume_tel = $data['resume_tel'];
        $resume->resume_email = $data['resume_email'];
        $resume->time = time();

//        $resume->update();

        if( $resume->save() ){
            $res['status'] = 1;
            $res['msg'] = '保存成功!';
        }else{
            $res['status'] = 0;
            $res['msg'] = '保存失败!';
        }
        exit( json_encode($res) );


    }



    // 修改  工作经历
    public function actionGzjl(){
        $data = Yii::$app->request->post();
        $user_id =  $_SESSION['user']['user_id'];

//        //个人简历 表
//        // 当亲登录的用户 id     从  session|cooike 中取 id
        $resume = Resume::find()->where( ['user_id'=>$user_id])->one();

        $resume->company_name = $data['company_name'];
        $resume->position_name = $data['position_name'];
        $resume->begin_year = $data['begin_year'];
        $resume->begin_month = $data['begin_month'];
        $resume->end_year = $data['end_year'];
        $resume->end_month = $data['end_month'];
        $resume->time = time();
//
//        $resume->update();

        if( $resume->save() ){
            $res['status'] = 1;
            $res['msg'] = '保存成功!';
        }else{
            $res['status'] = 0;
            $res['msg'] = '保存失败!';
        }
        exit( json_encode($res) );


    }


    // 自我描述
    public function actionMy_content(){
        $data = Yii::$app->request->post();
        $user_id =  $_SESSION['user']['user_id'];
//        //个人简历 表
//        // 当亲登录的用户 id     从  session|cooike 中取 id
        $resume = Resume::find()->where( ['user_id'=>$user_id])->one();
        $resume->my_content = $data['my_content'];
        $resume->time = time();

        if( $resume->save() ){
            $res['status'] = 1;
            $res['msg'] = '保存成功!';
        }else{
            $res['status'] = 0;
            $res['msg'] = '保存失败!';
        }
        exit( json_encode($res) );

    }


}
 ?>